const Koa = require('koa')
const app = new Koa()
const { connect, initSchemas } = require('./database/init.js')
const mongoose = require('mongoose')
const bodyParser = require('koa-bodyparser')
const Router = require('koa-router')
// 跨域处理
const cors = require('koa2-cors')
app.use(cors())

// 注册bodyParser
app.use(bodyParser())

// 装载所有子路由
let user = require('./appApi/user.js')
let home = require('./appApi/home.js')
let goods = require('./appApi/goods.js')
let router = new Router()
router.use('/user', user.routes())
router.use('/home', home.routes())
router.use('/goods', goods.routes())

// 加载路由中间件
app.use(router.routes())
app.use(router.allowedMethods())

;(async () => {
  await connect()
  initSchemas()
})()

// 页面主入口
app.use(async (ctx) => {
  ctx.body = `<h1>hello world</h1>`
})
// 监听端口
app.listen(3000, () => {
  console.log('listen 3000')
})
